package Lib
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;

	public class VideoPlayer
	{
		private var videoURL:String = "Video/1145.flv";
		private var connection:NetConnection;
		private var stream:NetStream;
		private static var videoPlayer:VideoPlayer = null;
		
		public function VideoPlayer() {
			connection = new NetConnection();
			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,securityErrorHandler);
			connection.connect(null);
		}
		
		public static function instance():VideoPlayer
		{
			if(videoPlayer == null)
				videoPlayer = new VideoPlayer();
			return videoPlayer;
		}
		
		private function netStatusHandler(event:NetStatusEvent):void {
			switch (event.info.code) {
				case "NetConnection.Connect.Success":
					connectStream();
					break;
				case "NetStream.Play.StreamNotFound":
					trace("Unable to locate video: " + videoURL);
					break; 
			}
		}
		
		private function connectStream():void { 
			stream = new NetStream(connection);
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			var video:Video = new Video();
			video.attachNetStream(stream);
			var clientobj:Object = new Object();
			clientobj.onMetaData = new Function; 
			stream.client=clientobj;
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event); 
		}
		
		private function asyncErrorHandler(event:AsyncErrorEvent):void {
			
		}
		
		public function startPlayVideo():void
		{
			this.stream.play(videoURL);
		}
		
		public function stopPlayVideo():void
		{
			this.stream.close();
		}
		
		
	}
}